$(function() {
	 initradio('type',1);
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/StatisticsOtherCollect/getDatas",
		paramFunc: paramFunc,	
		afterFunc:afterFunc,
		hasCheckbox: false 
	});
	laydate.render({
	    elem: '#start_time', //指定元素
	    trigger: 'click'
	});
	laydate.render({
	    elem: '#end_time', //指定元素
	    trigger: 'click'
	});
	
	// exportEvent(); 
	$('#start_time').change(function(event) {
		if($('#start_time').val()!='' && $('#end_time').val()!=''){
			$('.searchButton').click();
		}
	});
	$('#end_time').change(function(event) {
		if($('#start_time').val()!='' && $('#end_time').val()!=''){
			$('.searchButton').click();
		}
	});

    $("input[type='radio']").click(function(event) {
       var type =  $("input[name='type']:checked").val();
        if(type =='1'){
        	$('#usercode').hide();
        	$('#username').hide();
            $('#dept_name').html('部门');
            $('#dept_name').attr('name','_dept_name');
            $("#usercode").remove();
            $("#dept_name_s").hide();
            $("#username_s").hide();
            $('.searchButton').click();
            
        }else{
        	$('#usercode').show();
        	$('#username').show();
            $('#dept_name').html('教师姓名');
            $('#dept_name').attr('name','_username');
            $("#dept_name").before('<th name="_usercode" id="usercode"> 工号 </th>');
            $("#dept_name_s").show();
            $("#username_s").show();
            $('.searchButton').click();
            
        }
    });

}); 

//添加默认选中按钮
function initradio(rName,rValue){
    var rObj = document.getElementsByName(rName);
    for(var i = 0;i < rObj.length;i++){
        if(rObj[i].value == rValue){
            rObj[i].checked =  'checked';
        }
    }
}

var paramFunc = function() { 
	var data = getFormJson();
	return data; 
};


var afterFunc = function(){
    $("#table tbody tr").each(function() {
    	var type =  $("input[name='type']:checked").val();
        var data = $("#table").getOpts().datas[$(this).index()];
        console.log(data);
        if(type == 1){
        	if(data.is_total == 1){
        		   $(this).children("td:nth-child(4)").html($(this).children("td:nth-child(4)").html());
        		   $(this).children("td:nth-child(5)").html($(this).children("td:nth-child(5)").html());
        		   $(this).children("td:nth-child(6)").html($(this).children("td:nth-child(6)").html());
        		   $(this).children("td:nth-child(7)").html($(this).children("td:nth-child(7)").html());
        		   $(this).children("td:nth-child(9)").html($(this).children("td:nth-child(9)").html());
        		   $(this).children("td:nth-child(8)").html($(this).children("td:nth-child(8)").html());
        	}else if(data.is_total == 0){
        		// 教学督导专题教研次数
		        $(this).children("td:nth-child(4)").attr('onClick', 'gotoAttend(this)');
		        $(this).children("td:nth-child(4)").attr('dept_id', data.dept_id);
		        $(this).children("td:nth-child(4)").attr('type', 1);
		        $(this).children("td:nth-child(4)").html("<a>"+$(this).children("td:nth-child(4)").html()+"</a>");

		         // 督导教学巡(检)查情况记录次数
		        $(this).children("td:nth-child(5)").attr('onClick', 'gotoAttend(this)');
		        $(this).children("td:nth-child(5)").attr('dept_id', data.dept_id);
		        $(this).children("td:nth-child(5)").attr('type', 2);
		        $(this).children("td:nth-child(5)").html("<a>"+$(this).children("td:nth-child(5)").html()+"</a>");


		         // 督导巡考情况记录次数	
		        $(this).children("td:nth-child(6)").attr('onClick', 'gotoAttend(this)');
		        $(this).children("td:nth-child(6)").attr('dept_id', data.dept_id);
		        $(this).children("td:nth-child(6)").attr('type', 3);
		        $(this).children("td:nth-child(6)").html("<a>"+$(this).children("td:nth-child(6)").html()+"</a>");

		         // 教学状态专项检查次数	
		        $(this).children("td:nth-child(7)").attr('onClick', 'gotoAttend(this)'); 
		        $(this).children("td:nth-child(7)").attr('dept_id', data.dept_id);
		        $(this).children("td:nth-child(7)").attr('type', 4);
		        $(this).children("td:nth-child(7)").html("<a>"+$(this).children("td:nth-child(7)").html()+"</a>");


		        //教学信息员专题调查次数	
		        $(this).children("td:nth-child(9)").attr('onClick', 'gotoAttend(this)'); 
		        $(this).children("td:nth-child(9)").attr('dept_id', data.dept_id);
		        $(this).children("td:nth-child(9)").attr('type', 5);
		        $(this).children("td:nth-child(9)").html("<a>"+$(this).children("td:nth-child(9)").html()+"</a>");

		        //学生顶岗实习巡查次数	
		        $(this).children("td:nth-child(8)").attr('onClick', 'gotoAttend(this)'); 
		        $(this).children("td:nth-child(8)").attr('dept_id', data.dept_id);
		        $(this).children("td:nth-child(8)").attr('type', 6);
		        $(this).children("td:nth-child(8)").html("<a>"+$(this).children("td:nth-child(8)").html()+"</a>");

		        //听课表汇总
		        // $(this).children("td:nth-child(9)").attr('onClick', 'gotoAttend(this)'); 
		        // $(this).children("td:nth-child(9)").attr('dept_name', data.dept_name);
		        // $(this).children("td:nth-child(9)").attr('type', 6);
		        // $(this).children("td:nth-child(9)").html("<a>"+$(this).children("td:nth-child(9)").html()+"</a>");

        	}
	       

        }

    });
}

function Export(){		
	if($('#start_time').val()==''){
		window.parent.topMessagePrompt('error','请输入起始时间');
		return;
	}
	if($('#end_time').val()==''){
		window.parent.topMessagePrompt('error','请输入终止时间');
		return;
	}		
	var data = getFormJson();
	// window.open($("#url").val()+'Admin/StatisticsFeedBack/StatisticsOther/-1/'+$('#start_time').val()+'/'+$('#end_time').val()+'/true');
	var msg = "请确认是否导出?";
    swal({
        title: "请确认",
        text: msg,
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        confirmButtonText: "确定",
        cancelButtonText: '取消',
        confirmButtonColor: "#ec6c62"
    }, function(isConfirm) {
        if(isConfirm) {
            var load_alert=loadAlert('数据导出中，请稍后...');
            $.ajax({
                type: "post",
                url: "Admin/StatisticsOtherCollect/getDatas/1/true",
                data:data,
                async: true,
                dataType:'json',
                success: function(data) {
                    closeAlert(load_alert);
                    if(data.status) {
                        location.href=getRootPath()+'/'+data.v;
                    } else {
                        window.parent.topMessagePrompt('error',ret.msg);
                    }
                }
            });
        }
    });
}


function gotoAttend(obj){	
	var url=$("#url").val();
	var type = $(obj).attr('type');
	var dept_id = $(obj).attr('dept_id');

	if(type != 6){
		if($('#start_time').val()==''){
			window.parent.topMessagePrompt('error','请输入起始时间');
			return;
		}
		if($('#end_time').val()==''){
			window.parent.topMessagePrompt('error','请输入终止时间');
			return;
		}
	}
	
	if(type==1){
		showDialogModal(url+'Admin/StatisticsTable1/Export/'+dept_id+'/'+$('#start_time').val()+'/'+$('#end_time').val());
	}else if(type==2){
		showDialogModal(url+'Admin/StatisticsTable2/Export/'+dept_id+'/'+$('#start_time').val()+'/'+$('#end_time').val());
	}else if(type==3){
		showDialogModal(url+'Admin/StatisticsTable3/Export/'+dept_id+'/'+$('#start_time').val()+'/'+$('#end_time').val());
	}else if(type==4){
		showDialogModal(url+'Admin/StatisticsTable4/Export/'+dept_id+'/'+$('#start_time').val()+'/'+$('#end_time').val());
	}else if(type==5){
		showDialogModal(url+'Admin/StatisticsTable6/Export/'+dept_id+'/'+$('#start_time').val()+'/'+$('#end_time').val());
	}else if(type==6){
		showDialogModal(url+'Admin/StatisticsTable5/Index/'+dept_id);
	}

	
}